<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="商品名称" field="goods_name">
            <a-input v-model="searchForm.goods_name" placeholder="请输入商品名称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="订单类型" field="order_type">
            <sa-select v-model="searchForm.order_type" dict="app_goods_type" placeholder="请选择订单类型" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="支付方式" field="pay_method">
            <sa-select v-model="searchForm.pay_method" dict="app_pay_way" placeholder="请选择支付方式" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="订单状态" field="order_status">
            <sa-select
              v-model="searchForm.order_status"
              dict="app_order_status"
              placeholder="请选择订单状态"
              allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="16">
          <a-form-item field="create_time" label="下单时间">
            <a-range-picker v-model="searchForm.create_time" showTime style="width: 100%" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #order_no="{ record }">
        <p>{{ record.create_time }}</p>
        <p>{{ record.order_no }}</p>
      </template>
      <template #goods_name="{ record }">
        <p class="text-wrap line-clamp-2 text-xs">{{ record.goods_name }}</p>
      </template>
      <template #user_id="{ record }">
        <p>{{ record.account }} | {{ record.user_id }}</p>
      </template>
      <!-- 操作列 -->
      <template #operationBeforeExtend="{ record }">
        <a-link type="primary" @click="detailAction(record)"> <icon-eye />详情 </a-link>
        <a-link
          type="primary"
          v-if="record.order_status == 2 && record.order_type == 2"
          v-auth="['/app/saiuser/order/AppOrder/delivery']"
          @click="deliveryAction(record)">
          <icon-export />发货
        </a-link>
      </template>
    </sa-table>

    <!-- 详情 -->
    <detail-form ref="detailRef" @success="refresh" />
    <!-- 发货 -->
    <delivery-form ref="deliveryRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import DetailForm from './detail.vue'
import DeliveryForm from './delivery.vue'
import api from '../../api/order/order'

// 引用定义
const crudRef = ref()
const detailRef = ref()
const deliveryRef = ref()

// 搜索表单
const searchForm = ref({
  order_type: '',
  goods_name: '',
  pay_method: '',
  order_status: '',
  create_time: null,
})

const detailAction = async (record) => {
  detailRef.value?.open(record.id)
}

const deliveryAction = async (record) => {
  deliveryRef.value?.open(record.id)
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  operationColumnWidth: 200,
  delete: {
    show: true,
    auth: ['/app/saiuser/order/AppOrder/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '下单时间|订单号', dataIndex: 'order_no', width: 180 },
  { title: '商品图片', dataIndex: 'cover_image', type: 'image', size: 48, width: 100 },
  { title: '商品名称', dataIndex: 'goods_name', width: 250 },
  { title: '用户信息', dataIndex: 'user_id', width: 140 },
  { title: '订单类型', dataIndex: 'order_type', type: 'dict', dict: 'app_goods_type', width: 100 },
  { title: '支付金额', dataIndex: 'pay_price', width: 100 },
  { title: '支付方式', dataIndex: 'pay_method', type: 'dict', dict: 'app_pay_way', width: 100 },
  { title: '支付时间', dataIndex: 'pay_time', width: 180 },
  { title: '订单状态', dataIndex: 'order_status', type: 'dict', dict: 'app_order_status', width: 120 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
